package com.xsq.rpc.center.server.controller;

import com.xsq.rpc.center.server.config.DruidDataSourceHandler;
import com.xsq.rpc.center.server.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @ClassName TestController
 * @Description TODO
 * @Author xsq
 * @Date 2020/6/11 17:41
 **/
@RestController
public class TestController {

    @Autowired
    private DruidDataSourceHandler druidDataSourceHandler;

    @GetMapping(value = "/test")
    public ResponseEntity showData() {
        try {
            Connection connection = druidDataSourceHandler.getDataSource().getConnection();
            String sql = "select * from user where id=?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setLong(1, 1L);
            ResultSet rs = statement.executeQuery();
            User user = null;
            while (rs.next()) {
                long id = rs.getLong("id");
                String name = rs.getString("name");
                int age = rs.getInt("age");
                String phone = rs.getString("phone");
                String remark = rs.getString("remark");
                user = new User();
                user.setId(id);
                user.setName(name);
                user.setAge(age);
                user.setPhone(phone);
                user.setRemark(remark);
            }
            return ResponseEntity.ok(user);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }
}
